clear all
set printcolor gs1, permanently
set more off
global dirout ""
global dirgeolocalized ""
global dircenso ""
global dirobs ""


use "$dirobs/eic_Puebla_2015.dta", clear
append using "$dirobs/eic_Tlaxcala_2015.dta"
gen school_public=0 if goes_school==1
replace school_public=1 if school_transp_bus==1 | school_transp_metro==1
gen work_public=0 if goes_work==1
replace work_public=1 if work_transp_bus==1 | work_transp_metro==1
replace income=. if goes_work==0
collapse (mean) income school_public work_public [w=factor], by(state municipio)
gen str2 CVE_ENT = string(state,"%02.0f")
gen str3 CVE_MUN = string(municipio,"%03.0f")
drop state municipio
save "$dirobs/TEMP_EIC.dta", replace

use "$dirobs/BDIF.dta", clear
keep if year==2017
keep state municipio credit debit atm_trans population branches atms adults
collapse (sum) debit credit atm_trans population branches atms adults, by(state municipio)
rename state CVE_ENT
rename municipio CVE_MUN
save "$dirobs/TEMP_BDIF.dta", replace


use "$dirgeolocalized/Puebla/Datos_Geolocalizados_Puebla.dta", clear
append using "$dirgeolocalized/Puebla/Datos_Geolocalizados_Puebla_August"
keep client_uuid is_cash_trip year month
gen trips=1
gen cash=0
replace cash=1 if is_cash_trip=="TRUE" | is_cash_trip=="true"
collapse (sum) cash trips, by(client year)
save "$dirgeolocalized/Puebla/temp.dta", replace


use "$dirgeolocalized/Puebla/temp.dta", clear
merge m:1 client using "$dirgeolocalized/Puebla/users_geolocalized_Puebla.dta", keep(match) nogenerate
egen user=group(client)
drop client
gen present=1
drop if year==2016

tsset user year
tsfill, full
bysort user: egen max_ID=max(ID_origin)
drop ID_origin
rename max_ID ID_origin
replace present=0 if present==.
drop confidence count
reshape wide cash trips present, i(ID_origin user) j(year)
save "$dirgeolocalized/Puebla/temp.dta", replace


use "$dirgeolocalized/Puebla/temp.dta", clear
drop cash2018
drop if present2017==0
drop present2017
gen share_cash=cash2017/trips2017
replace trips2018=0 if present==0
gen d_trips=2*(trips2018-trips2017)/(trips2018+trips2017)
rename ID_origin ID
merge m:1 ID using "$dircenso/censo_Puebla_geo.dta", nogenerate keep(match) ///
keepusing(CVE_ENT NOM_ENT CVE_MUN NOM_MUN CVE_LOC NOM_LOC CVE_AGEB CVE_MZA POBTOT VIVTOT GRAPROES VPH_AUTOM VPH_CEL VPH_INTER)
compress

merge m:1 CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA using "$dirgeolocalized/denue_bancos.dta"
drop if _merge==2
drop _merge
replace count_banks=0 if count_banks==.
gen bank=(count_banks!=0 & count_banks!=.)

gen sVPH_AUTOM = VPH_AUTOM/VIVTOT
gen sVPH_CEL = VPH_CEL/VIVTOT
gen sVPH_INTER = VPH_INTE/VIVTOT
gen lntrips2017=ln(trips2017)

egen id=group(CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA)
egen id2=group(CVE_ENT CVE_MUN)

bysort CVE_ENT CVE_MUN CVE_LOC CVE_AGEB: egen bank2=max(bank)

drop bank
rename bank2 bank
label var bank "Bank in basic geo. area"
label var GRAPROES "Years of Education"
label var present "Present in 2018"

		
bysort CVE_ENT NOM_ENT CVE_MUN NOM_MUN: egen tPOBTOT=sum(POBTOT)
bysort CVE_ENT NOM_ENT CVE_MUN NOM_MUN: egen ttrips2017=sum(trips2017)
gen n=1
bysort CVE_ENT NOM_ENT CVE_MUN NOM_MUN: egen users=sum(n)
bysort CVE_ENT NOM_ENT CVE_MUN NOM_MUN: egen total_banks=sum(count_banks)

collapse (mean) bank total_banks users present2018 share_cash d_trips GRAPROES tPOBTOT ///
 sVPH_AUTOM sVPH_CEL sVPH_INTER ttrips2017 [w=trips2017], by(CVE_ENT NOM_ENT CVE_MUN NOM_MUN)
rename tPOBTOT POBTOT
rename ttrips2017 trips2017

merge 1:1 CVE_ENT CVE_MUN using "$dirobs/TEMP_BDIF.dta", nogenerate keep(match)
merge 1:1 CVE_ENT CVE_MUN using "$dirobs/TEMP_EIC.dta", nogenerate keep(match)
replace NOM_MUN="Cholula" if NOM_MUN=="San Andrés Cholula"

gen debit_p=(debit/population)
gen credit_p=(credit/population)
gen atm_trans_p=(atm_trans/population)
gen branches_p=(branches/population)*1000
gen atms_p=(atms/population)*1000
gen banks_p=(total_banks/population)*1000
gen income_usd=income/17.2


		#delimit
		twoway 
		(scatter present2018 income_usd if users>5, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit present2018 income_usd if users>5, lcolor(navy) lwidth(thick))
		, ytitle("Probability of returning after ban", height(5)) title(" ") 
		xtitle("Average income per capita - monthly (USD)")
		legend(off) graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureD4a.eps", replace

		#delimit
		twoway 
		(scatter present2018 atms_p if users>5, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit present2018 atms_p if users>5, lcolor(navy) lwidth(thick))
		, ytitle("Probability of returning after ban", height(5)) title(" ") 
		xtitle("ATMs per capita")
		legend(off) graphregion(color(white)  margin(r+10)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureD4b.eps", replace
		
		#delimit
		twoway 
		(scatter present2018 debit_p if users>5, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit present2018 debit_p if users>5, lcolor(navy) lwidth(thick))
		, ytitle("Probability of returning after ban", height(5)) title(" ") 
		xtitle("Debit cards per capita")
		legend(off) graphregion(color(white)  margin(r+5)) plotregion(fcolor(white));
		#delimit cr		
		graph export "$dirout/FigureD4c.eps", replace
		
		#delimit
		twoway 
		(scatter present2018 credit_p if users>5, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit present2018 credit_p if users>5, lcolor(navy) lwidth(thick))
		, ytitle("Probability of returning after ban", height(5)) title(" ") 
		xtitle("Credit cards per capita") 
		legend(off) graphregion(color(white) margin(r+10)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureD4d.eps", replace
		
		

